import os
import shutil
import yaml
import psutil

from ktransformers.server.config.singleton import Singleton


class Config(metaclass=Singleton):
    """Singleton pattern Config class, used to get all configurations."""

    CONFIG_FILE_NAME = "config.yaml"

    @staticmethod
    def load() -> dict:
        """load config file

        Returns:
            dict: all configs
        """
        base_path: str = os.path.dirname(__file__)
        config_yaml: str = os.path.join(base_path, Config.CONFIG_FILE_NAME)

        with open(config_yaml, "r", encoding="utf-8") as fp:
            config = yaml.safe_load(fp)
        return config

    def __init__(self):
        cfg = Config.load()

        # web configs
        self.web_configs: dict = cfg.get("web", {})
        self.upload_dir = self.web_configs.get("upload_dir", "")
        self.output_dir = self.web_configs.get("output_dir", "")
        self.scene_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "scene")
   
        # db configs
        self.db_configs: dict = cfg.get("db", {})
        self.mysql_type = self.db_configs.get("type", "")
        self.mysql_username = self.db_configs.get("username", "")
        self.mysql_password = self.db_configs.get("password", "")
        self.mysql_host = self.db_configs.get("host", "")
        self.mysql_port = self.db_configs.get("port", "")
        self.mysql_pool_size = self.db_configs.get("pool_size")
        self.mysql_database = self.db_configs.get("database", "")

        # email configs
        self.email_configs: dict = cfg.get("email", {})
        self.sender_email = self.email_configs.get("sender_email", "") # 发件人邮箱
        self.receiver_email = self.email_configs.get("receiver_email", "") # 收件人邮箱
        self.password = self.email_configs.get("password", "") # 发件人邮箱的授权密码
        self.smtp_server = self.email_configs.get("smtp_server", "")
        self.smtp_port = self.email_configs.get("smtp_port", "")